package com.reactim.message.config;

/**
 * 配置集成测试运行器
 * 验证所有配置场景的正确性，不依赖Spring Boot上下文
 */
public class ConfigurationIntegrationTestRunner {
    
    public static void main(String[] args) {
        System.out.println("=== 开始配置集成测试 ===");
        
        try {
            testConfigurationLoading();
            testConfigurationValidation();
            testConfigurationScenarios();
            testTestEnvironmentConfiguration();
            
            System.out.println("=== ✅ 所有配置集成测试通过！ ===");
            
        } catch (Exception e) {
            System.err.println("=== ❌ 配置集成测试失败: " + e.getMessage() + " ===");
            e.printStackTrace();
            System.exit(1);
        }
    }
    
    private static void testConfigurationLoading() {
        System.out.println("1. 测试配置加载...");
        
        // 测试ReactimConfig加载
        ReactimConfig reactimConfig = new ReactimConfig();
        assert reactimConfig.getMessage() != null : "消息配置不应为null";
        assert reactimConfig.getConnection() != null : "连接配置不应为null";
        assert reactimConfig.getPerformance() != null : "性能配置不应为null";
        
        // 测试PerformanceTestConfig加载
        PerformanceTestConfig performanceTestConfig = new PerformanceTestConfig();
        assert performanceTestConfig.getMaxConnections() > 0 : "最大连接数应大于0";
        assert performanceTestConfig.getOperationTimeoutSeconds() > 0 : "操作超时时间应大于0";
        assert performanceTestConfig.getBatchSizes() != null : "批次大小数组不应为null";
        assert performanceTestConfig.getThreadCounts() != null : "线程数数组不应为null";
        
        System.out.println("   ✅ 配置加载测试通过");
    }
    
    private static void testConfigurationValidation() {
        System.out.println("2. 测试配置验证...");
        
        // 测试有效配置
        PerformanceTestConfig validConfig = new PerformanceTestConfig();
        validConfig.setMaxConnections(50000);
        validConfig.setOperationTimeoutSeconds(60);
        validConfig.setBatchSizes(new int[]{1000, 5000, 10000});
        validConfig.setThreadCounts(new int[]{5, 10, 20});
        assert validConfig.isValid() : "有效配置应该通过验证";
        
        // 测试无效配置
        PerformanceTestConfig invalidConfig = new PerformanceTestConfig();
        invalidConfig.setMaxConnections(-1);
        assert !invalidConfig.isValid() : "无效配置应该被识别";
        
        invalidConfig.setMaxConnections(100000);
        invalidConfig.setBatchSizes(null);
        assert !invalidConfig.isValid() : "null数组配置应该被识别为无效";
        
        invalidConfig.setBatchSizes(new int[]{-1, 1000});
        assert !invalidConfig.isValid() : "包含负数的数组配置应该被识别为无效";
        
        System.out.println("   ✅ 配置验证测试通过");
    }
    
    private static void testConfigurationScenarios() {
        System.out.println("3. 测试配置场景...");
        
        // 测试默认配置场景
        PerformanceTestConfig defaultConfig = new PerformanceTestConfig();
        assert defaultConfig.isValid() : "默认配置应该有效";
        assert defaultConfig.getMaxConnections() == 100000 : "默认最大连接数应为100000";
        assert defaultConfig.getOperationTimeoutSeconds() == 30 : "默认操作超时应为30秒";
        
        // 测试最小配置场景
        PerformanceTestConfig minimalConfig = new PerformanceTestConfig();
        minimalConfig.setMaxConnections(1);
        minimalConfig.setOperationTimeoutSeconds(1);
        minimalConfig.setBatchSizes(new int[]{1});
        minimalConfig.setThreadCounts(new int[]{1});
        assert minimalConfig.isValid() : "最小配置应该有效";
        
        // 测试自定义配置场景
        PerformanceTestConfig customConfig = new PerformanceTestConfig();
        customConfig.setMaxConnections(75000);
        customConfig.setOperationTimeoutSeconds(45);
        customConfig.setCleanupAfterTest(false);
        customConfig.setBatchSizes(new int[]{2000, 4000, 8000});
        customConfig.setThreadCounts(new int[]{8, 16, 32});
        assert customConfig.isValid() : "自定义配置应该有效";
        assert customConfig.getMaxConnections() == 75000 : "自定义最大连接数应为75000";
        assert !customConfig.isCleanupAfterTest() : "自定义清理设置应为false";
        
        System.out.println("   ✅ 配置场景测试通过");
    }
    
    private static void testTestEnvironmentConfiguration() {
        System.out.println("4. 测试环境配置验证...");
        
        // 测试ReactimConfig的测试环境适配
        ReactimConfig reactimConfig = new ReactimConfig();
        ReactimConfig.ConnectionProperties connectionProps = reactimConfig.getConnection();
        
        // 验证安全配置适合测试环境
        ReactimConfig.ConnectionProperties.SecurityProperties securityProps = connectionProps.getSecurity();
        assert !securityProps.isEnableEncryption() : "测试环境应该禁用加密";
        assert !securityProps.isEnableValidation() : "测试环境应该简化验证";
        assert !securityProps.isEnableAccessControl() : "测试环境应该简化访问控制";
        
        // 验证监控配置适合测试环境
        ReactimConfig.ConnectionProperties.MonitoringProperties monitoringProps = connectionProps.getMonitoring();
        assert !monitoringProps.isSendActualHeartbeat() : "测试环境应该禁用实际心跳";
        assert !monitoringProps.isEnableDashboard() : "测试环境应该禁用仪表板";
        
        // 验证Redis键前缀包含测试标识
        String keyPrefix = connectionProps.getRedis().getKeyPrefix();
        assert keyPrefix.contains("test") : "Redis键前缀应该包含测试标识: " + keyPrefix;
        
        // 验证性能测试配置的完整性
        PerformanceTestConfig performanceTestConfig = new PerformanceTestConfig();
        assert performanceTestConfig.getBatchSizes().length >= 3 : "应该支持多种批次大小";
        assert performanceTestConfig.getThreadCounts().length >= 3 : "应该支持多种线程数";
        
        // 验证配置的可扩展性
        int[] batchSizes = performanceTestConfig.getBatchSizes();
        int minBatchSize = Integer.MAX_VALUE;
        int maxBatchSize = Integer.MIN_VALUE;
        for (int batchSize : batchSizes) {
            minBatchSize = Math.min(minBatchSize, batchSize);
            maxBatchSize = Math.max(maxBatchSize, batchSize);
        }
        assert minBatchSize <= 1000 : "应该支持小批次测试: " + minBatchSize;
        assert maxBatchSize >= 10000 : "应该支持大批次测试: " + maxBatchSize;
        
        System.out.println("   ✅ 测试环境配置验证通过");
    }
}